package com.xqboss.apps.service.order;


import cn.hutool.core.bean.BeanUtil;
import com.xqboss.apps.domain.order.OrderMain;
import com.xqboss.apps.domain.user.YeepayWallet;
import com.xqboss.apps.dto.order.YeePayDivideDto;
import com.xqboss.apps.enums.order.DivideStatusEnum;
import com.xqboss.apps.service.user.YeepayWalletService;
import com.xqboss.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.order.OrderDivide;
import com.xqboss.apps.mapper.order.OrderDivideMapper;

import java.math.BigDecimal;


/**
 * 订单-分账(OrderDivide)表服务实现类
 *
 * @author makejava
 * @since 2023-12-18 15:35:26
 */
@Service
public class OrderDivideService extends ServiceImpl<OrderDivideMapper, OrderDivide> {

    @Autowired
    private YeepayWalletService yeepayWalletService;

    @Autowired
    @Lazy
    private OrderService orderService;

    @Autowired
    @Lazy
    private PayApplyService payApplyService;


    /**
     * 易宝分账
     * @param orderMain
     */
    public void yeePayDivideApply(OrderMain orderMain, BigDecimal sellerUserIncome) {
        // 获取易宝收款订单号
        YeePayDivideDto param = new YeePayDivideDto();
        param.setOrderId(orderMain.getId());
        param.setOrderNo(orderMain.getPayOrderNo());
        param.setOutTradeNo(payApplyService.getOrderPayApply(orderMain.getId()).getOutTradeNo());
        param.setAmount(sellerUserIncome);
        param.setStatus(DivideStatusEnum.WAIT);
        param.setUserId(orderMain.getSellerUserId());
        param.setRequestId(IdUtils.fastSimpleUUID());
        YeepayWallet yeepayWallet = yeepayWalletService.getUserWallet(orderMain.getSellerUserId());
        if (yeepayWallet != null) {
            param.setMemberNo(yeepayWallet.getMemberNo());
            yeepayWalletService.divideApply(param);
        }
        if (param.getStatus() == DivideStatusEnum.SUCCESS) {
            // 完结分账
            param.setRequestId(param.getRequestId()+"1");
            yeepayWalletService.divideComplete(param);
        }
        OrderDivide orderDivide = BeanUtil.copyProperties(param, OrderDivide.class);
        this.save(orderDivide);
    }


    /**
     * 易宝分账查询更新
     * @param orderId
     */
    public void yeePayDivideUpdate(Long orderId) {
        OrderDivide orderDivide = this.lambdaQuery().eq(OrderDivide::getOrderId, orderId).one();
        if (orderDivide.getStatus() != DivideStatusEnum.PROCESSING) {
            return;
        }
        YeePayDivideDto param = BeanUtil.copyProperties(orderDivide, YeePayDivideDto.class);
        param.setOrderNo(orderService.getById(orderId).getPayOrderNo());
        param.setOutTradeNo(payApplyService.getOrderPayApply(orderId).getOutTradeNo());
        param.setRequestId(IdUtils.fastSimpleUUID());
        yeepayWalletService.divideQuery(param);
        orderDivide = BeanUtil.copyProperties(param, OrderDivide.class);
        this.updateById(orderDivide);
    }
}
